﻿using System;
using System.Data;
using eTMS.Controllers.LoaiDoiTuongTroCapService;
using eTMS.Controllers.KieuHuongTroCapService;
using System.Collections.Generic;
namespace eTMS.Controllers
{
    public class LoaiDoiTuongTroCapController : LoaiDoiTuongTroCapControllerBase
    {
        public LoaiDoiTuongTroCapController()
            : base()
        {
        }
        private DataTable _tblChiTietRemoved;
        private DataTable _tblChiTiet;
        public DataTable TableChiTiet
        {
            get
            {
                return _tblChiTiet;
            }
        }
        public void LoadChiTiet()
        {
            LoadChiTiet(LoaiDoiTuongTroCapObject.LoaiDoiTuongTroCap_ID);
        }
        private void LoadChiTiet(int LoaiDoiTuongTroCap_ID)
        {
            KieuHuongTroCapService.KieuHuongTroCapServiceClient kieuHuongTroCapServiceClient = new KieuHuongTroCapService.KieuHuongTroCapServiceClient();
            _tblChiTiet = kieuHuongTroCapServiceClient.SelectAllDSFilter("LoaiDoiTuongTroCap_ID="+LoaiDoiTuongTroCap_ID.ToString()).Tables[0];
            _tblChiTietRemoved = _tblChiTiet.Clone();
        }
        public void AddNewChiTiet()
        {
            if (_tblChiTiet == null)
            {
                LoadChiTiet(0);
            }
            DataRow row = _tblChiTiet.NewRow();
            row["KieuTroCap"] = 1;
            if (KhoanTienController.ListKhoanTienStatic.Count > 0)
                row["KhoanTien_ID"] = KhoanTienController.ListKhoanTienStatic[0].KhoanTien_ID;
            row["MucTroCap"] = 0;
            _tblChiTiet.Rows.Add(row);
        }
        public void RemoveChiTiet(List<DataRow> list)
        {
            if (_tblChiTietRemoved == null) _tblChiTietRemoved = _tblChiTiet.Clone();
            foreach (DataRow row in list)
            {
                _tblChiTietRemoved.ImportRow(row);
                _tblChiTiet.Rows.Remove(row);
            }
        }
        public KieuHuongTroCapEntity MapDataRowDetailToObject(DataRow row)
        {
            KieuHuongTroCapEntity o = new KieuHuongTroCapEntity();
            if (!row.IsNull("MucTroCap"))
                o.MucTroCap = ConvertNumber(row["MucTroCap"]);
            if (!row.IsNull("KieuTroCap"))
                o.KieuTroCap = ConvertByte(row["KieuTroCap"]);
            if (!row.IsNull("KhoanTien_ID"))
                o.KhoanTien_ID = (Int32)(row["KhoanTien_ID"]);
            if (!row.IsNull("LoaiDoiTuongTroCap_ID"))
                o.LoaiDoiTuongTroCap_ID = (Int32)(row["LoaiDoiTuongTroCap_ID"]);
            if (!row.IsNull("KieuHuongTroCap_ID"))
                o.KieuHuongTroCap_ID = (Int32)(row["KieuHuongTroCap_ID"]);
            return o;
        }
        public void SaveDetail()
        {
            KieuHuongTroCapService.KieuHuongTroCapServiceClient kieuHuongTroCapServiceClient =
                new KieuHuongTroCapService.KieuHuongTroCapServiceClient();
            foreach (DataRow row in _tblChiTietRemoved.Rows)
            {
                int ID = ConvertInt(row["KieuHuongTroCap_ID"]);
                if (ID > 0)
                    kieuHuongTroCapServiceClient.DeleteByID(ID);
            }
            foreach (DataRow row in _tblChiTiet.Rows)
            {
                KieuHuongTroCapEntity o = MapDataRowDetailToObject(row);
                o.LoaiDoiTuongTroCap_ID = LoaiDoiTuongTroCapObject.LoaiDoiTuongTroCap_ID;
                if (o.KieuHuongTroCap_ID > 0)
                    kieuHuongTroCapServiceClient.Update(o);
                else
                    kieuHuongTroCapServiceClient.Save(o);
            }
        }
    }
}
